package com.dhh.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.dhh.util.StringUtil;
import com.dhh.vo.FoodPlan;
import com.dhh.vo.PlanItem;

public class FoodPlanDAO {
	public static final String C_ROWID = "_id";
	public static final String C_PERIOD = "period";
	public static final String C_FOOD_NAME = "food_name";
	public static final String C_FOOD_GROUP = "food_group";
	public static final String C_SERVICE = "service";
	public static final String C_ENERGY = "energy";
	public static final String C_PARENT = "parent";
	
	private DataBaseHelper db;
	
	public FoodPlanDAO(Context ctx){
		db = new DataBaseHelper(ctx);
	}
		
	public boolean removePlanItem(String id){
		boolean isSuc = false;
		
		try{
			db.getWritableDatabase().delete("daily_food_plan_item", "_id=?",new String[] {id});
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return isSuc;
	}
	
	public PlanItem getPlanItem(String id){
		return null;
	}
	
	public List<PlanItem> listAll(){
		List<PlanItem> lst = new ArrayList<PlanItem>();
		try{
			String[] columns = {C_ROWID,C_PERIOD,C_FOOD_NAME,C_FOOD_GROUP,C_SERVICE,C_ENERGY,C_PARENT}; 
			Cursor cursor = db.getReadableDatabase().query("daily_food_plan_item", columns, null, null, null,null, null);
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					PlanItem item = new PlanItem(cursor.getString(0),cursor.getString(1),StringUtil.cutString(cursor.getString(2),0),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6));
					lst.add(item);
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return lst;
	}
	
	public List<PlanItem> listPlanItem(String periodType,String plan){
		List<PlanItem> lst = new ArrayList<PlanItem>();
		try{
			String whereSQL = "1=1";
			if(periodType!=null){
				whereSQL += " and "+C_PERIOD+"='"+periodType+"'";
			}
			if(plan!=null){
				whereSQL += " and "+C_PARENT+"='"+plan+"'";
			}
			String[] columns = {C_ROWID,C_PERIOD,C_FOOD_NAME,C_FOOD_GROUP,C_SERVICE,C_ENERGY,C_PARENT}; 
			Cursor cursor = db.getReadableDatabase().query(true, "daily_food_plan_item", columns, whereSQL,null, null, null, null, null);
			
			if(cursor!=null && cursor.getCount()>0){
				cursor.moveToFirst();
				do{
					PlanItem item = new PlanItem(cursor.getString(0),cursor.getString(1),StringUtil.cutString(cursor.getString(2),0),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6));
					lst.add(item);
				}while(cursor.moveToNext());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return lst;
	}
	
	public FoodPlan getFoodPlan(String date){
		FoodPlan plan = new FoodPlan();
		if(date==null || date.length()==0)
			return null;
		try{
			String whereSQL = "";
			Cursor cursor = db.getReadableDatabase().query(true, "daily_food_plan", FoodPlan.getColumns(), whereSQL,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				plan.setId(cursor.getString(0));
				plan.setDate(cursor.getString(1));
				plan.setGrain(cursor.getString(2));
				plan.setFruit(cursor.getString(3));
				plan.setVegetable(cursor.getString(4));
				plan.setProteinFood(cursor.getString(5));
				plan.setDairy(cursor.getString(6));
				plan.setBreakFast(listPlanItem(PlanItem.PERIOD_BREAKFAST, plan.getId()));
				plan.setLunch(listPlanItem(PlanItem.PERIOD_LUNCH, plan.getId()));
				plan.setDinner(listPlanItem(PlanItem.PERIOD_DINNER, plan.getId()));
				plan.setSnack(listPlanItem(PlanItem.PERIOD_SNACK, plan.getId()));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return plan;
	}
	
	public boolean addPlanItem(PlanItem item) {
		boolean isSuc = false;
		ContentValues initialValues = createContentValues(item);
		try{
			db.openDataBase();
			db.getWritableDatabase().insert("daily_food_plan_item", null, initialValues);
			isSuc = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		return isSuc;
	}
	
	private ContentValues createContentValues(PlanItem item) {
		ContentValues values = new ContentValues();
		if(item.getPeriod()!=null && item.getPeriod().trim().length()>0)
			values.put(PlanItem.C_PERIOD, item.getPeriod());
		if(item.getFoodName()!=null && item.getFoodName().trim().length()>0)
			values.put(PlanItem.C_FOOD_NAME, item.getFoodName());
		if(item.getFoodGroup()!=null && item.getFoodGroup().trim().length()>0)
			values.put(PlanItem.C_FOOD_GROUP, item.getFoodGroup());
		if(item.getQuatity()!=null && item.getQuatity().trim().length()>0)
			values.put(PlanItem.C_QUATITY, item.getQuatity());
		if(item.getEnergy()!=null && item.getEnergy().trim().length()>0)
			values.put(PlanItem.C_ENERGY, item.getEnergy());
		if(item.getParent()!=null && item.getParent().trim().length()>0)
			values.put(PlanItem.C_PARENT, item.getParent());
		return values;
	}
	
	public void open(){
		db.openDataBase();
	}
	
	public void close(){
		db.close();
	}
}
